package com.kdtech.analyse.app;
import com.kd.crawler.common.utils.UrlUtils;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.AbstractMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

public class SinaAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://news.sina.cn/news_zt/threeyears\\?vt=4&pos=[0-9]*",
//		"http://toutiao.com/i[0-9]*/",
//		"http://toutiao.com/a[0-9]*/.*",
//		"http://toutiao.com/.*/[0-9]{10,}/.*",
//		"http://m.toutiao.com/item/[0-9]{10,}/",
//		"http://toutiao.com/user/5408632624/subscribe/",
	};

	public static HashMap<String, String> getLinks(String url)
	{

		HashMap<String, String> urlMap = new HashMap<String, String>();
		try {
			if(url.startsWith("http://m.toutiao.com/?")){
				String channel = StringUtils.substringAfter(url,"#channel=");
				if(channel.contains("&")) channel = StringUtils.substringBefore(channel, "&");
				String u = "http://m.toutiao.com/list/?tag="+channel+"&ac=wap&item_type=4&count=50&format=json&min_behot_time=0";
//				UrlMeta urlMeta = CrawlHTML.responseToURL(u);
				UrlMeta urlMeta = null;
				JSONObject json = JSONObject.parseObject(urlMeta.getHtml());
				String html = json.getString("html");
				urlMeta.setHtml(html);
//				return CrawlHTML.getCommonLinkListByHtml(urlMeta);
				return null;
//				http://m.toutiao.com/?W2atIF=1#channel=news_hot
//				http://m.toutiao.com/list/?tag=news_hot&ac=wap&item_type=4&count=20&format=json&min_behot_time=0
			}else{
				String board = StringUtils.substringBetween(url, "com/", "/");
				if(StringUtils.isBlank(board))
				{
					board = "";
				}

				String realUrl = "http://toutiao.com/api/article/recent/?source=2&count=20&category=" + board + "&max_behot_time=1444984749.52&utm_source=toutiao&offset=0";
				String detailUrl = "";

				UrlMeta urlMeta = null;
				if(urlMeta == null || StringUtils.isBlank(urlMeta.getHtml())){
					return null;
				}
				String htmltxt = urlMeta.getHtml();
				Matcher matcher = Pattern.compile("\"share_url\":.{50,120}\", \"id\":").matcher(htmltxt);
				while(matcher.find())
				{
					detailUrl = StringUtils.substring(matcher.group(), 14, -8);
					urlMap.put(detailUrl, "");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return urlMap;
	}

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String domain = UrlUtils.getDomain(url);
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if(!"toutiao.com".equals(domain)){
//			if(AnalyseMap.getInstance().isDetailPage(url)){
//				AbstractMeta meta = AnalyseMap.getInstance().parserHtml(urlMeta);
//				author = meta.getAuthor();
//				title =meta.getTitle();
//				content = meta.getContent();
//				date = meta.getDate();
//			}
		}
			if(StringUtils.isBlank(title)){
				title =doc.select("ul.h_nav_items").text();
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("span.time").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("time").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("#source").text());
			}
			if(StringUtils.isBlank(content)){
				content =HtmlCleaner.getContentHtml(url, doc.select("div.p_top_pic"));
			}
			if(StringUtils.isBlank(content)){
				content =HtmlCleaner.getContentHtml(url, doc.select("article"));
			}
			if(StringUtils.isBlank(author)){
				author=JSoupUtils.matchAuthor(doc, "来源：");
			}
			if(StringUtils.isBlank(author)){
				author = doc.select("#source").text();
			}

		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}
/*
	public static void main(String[] args) {
//		Matcher m = Pattern.compile("http://toutiao.com/(?!tag[0-9]+)").matcher("http://toutiao.com/tag84048389/");
//		System.out.println(m.find());
//		m = m.reset("http://toutiao.com/news_game/");
//		System.out.println(m.find());
//		getLinks("http://toutiao.com/news_game/");
//		HashMap<String, String> map = getLinks("http://toutiao.com/news_car/");
//		Set<Entry<String,String>> set = map.entrySet();
//		System.out.println("start");
//		for (Entry<String, String> entry : set) {
//			System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
//		}
//		System.out.println("end");

		SinaAppAnalyse analyse=new SinaAppAnalyse();
		String url="http://news.sina.cn/news_zt/threeyears?vt=4&pos=108";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	*/

	
}

